package com.meixiang.adapter.calendar;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.calaendar.ScheduleListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/6/16 17:01
 */
public class CalendarStyleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context context;

    private ScheduleListBean mData;

    public List<ScheduleListBean> mDayList = new ArrayList<>();
    private boolean isDelete = false;

    public CalendarStyleAdapter(Context context) {
        this.context = context;
    }


    public void addAll(List<ScheduleListBean> mDayList) {
        this.mDayList.addAll(mDayList);
//        this.mDayList=mDayList;
        this.notifyDataSetChanged();
    }

    public void clear() {
        this.mDayList.clear();
        this.notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_calendar_style, parent, false);
        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;


    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (mDayList != null) {
            mData = mDayList.get(position);
            ((ViewHolder) holder).mTvTitle.setText(mData.getTitle());
            //如果是备忘 只能删除 其他可以查看详细
            if (mData.getScheduleType().equals("MEMOS")) {
                ((ViewHolder) holder).mIvRight.setImageResource(R.mipmap.delete);
                isDelete = true;

            } else {
                isDelete = false;
                ((ViewHolder) holder).mIvRight.setImageResource(R.mipmap.enter);
            }

            if (mOnItemClickListener != null) {
//                holder.mIvRight.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        int pos = holder.getLayoutPosition();
//                        mOnItemClickListener.onItemClick(holder.itemView, pos, isDelete);
//
//                    }
//                });
                ((ViewHolder) holder).mIvRight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = holder.getLayoutPosition();
                        mOnItemClickListener.onItemClick(holder.itemView, pos, isDelete);
                    }
                });
            }

        }


    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position, boolean isDelete);
    }

    @Override
    public int getItemCount() {
        return mDayList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_title)
        TextView mTvTitle;
        @Bind(R.id.rl_calender_staging)
        RelativeLayout mRlCalenderStaging;
        @Bind(R.id.iv_right)
        ImageView mIvRight;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }


}
